package jghoman;
import com.twitter.finagle.Service;
import com.twitter.finagle.builder.ClientBuilder;
import com.twitter.finagle.thrift.ThriftClientRequest;
import com.twitter.util.FutureEventListener;
import org.apache.thrift.protocol.TBinaryProtocol;

import com.twitter.finagle.Service;
import com.twitter.finagle.builder.ClientBuilder;

import com.twitter.finagle.thrift.ThriftClientRequest;
import com.twitter.util.FutureEventListener;
import org.apache.thrift.protocol.TBinaryProtocol;

import scala.Function1;
import scala.runtime.BoxedUnit;

import com.twitter.finagle.Thrift;
import com.twitter.finagle.example.thriftscala.Hello;
import com.twitter.util.Await;
import com.twitter.util.Function;
import com.twitter.util.Future;

import java.net.InetSocketAddress;

import static scala.compat.java8.JFunction.func;

public final class ThriftClient {

    private ThriftClient() { }

    static Function1<Throwable, BoxedUnit> errorFunc = func(e -> {
        System.out.println("error: " + e.toString());
        return BoxedUnit.UNIT;
    });


    /**
     * Runs the example with given {@code args}.
     *
     * @param args the argument list
     */
    public static void main(String[] args) throws Exception {




        //#thriftclientapi

        Hello<Future> demoService = (Hello)Thrift.client().newIface("localhost:8080", Hello.class);
        Future<String> future1 = demoService.hi();


        future1.onSuccess(func(r -> {
            System.out.println("Received response: " + r);
            return BoxedUnit.UNIT;
        }));
        future1.onFailure(errorFunc);
        Await.ready(future1);

//        Hello.FutureIface client = (Hello.FutureIface) Thrift.client().newIface("localhost:8080", Hello.class);
//        Future<String> response = client.hi().onSuccess(new Function<String, BoxedUnit>() {
//            @Override
//            public BoxedUnit apply(String response) {
//                System.out.println("Received response: " + response);
//                return null;
//            }
//        });
//        Await.result(response);


        //#thriftclientapi

    }
}